home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / ugraph.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  5.0 KB  |  172 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  ugraph.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. #ifndef LEDA_UGRAPH_H
  17. #define LEDA_UGRAPH_H
  18.  
  19. #include <LEDA/graph.h>
  20.  
  21.  
  22. //-----------------------------------------------------------------------------
  23. // ugraph: base class for all undirected graphs
  24. //-----------------------------------------------------------------------------
  25.  
  26. class ugraph : public graph{
  27.  
  28. public:
  29.  
  30. /*
  31. edge new_edge(node v, node w, edge e1 ,edge e2, int dir1=0,int dir2=0)
  32. { GenPtr x; init_edge_entry(x);
  33.   return new_edge(v,w,e1,e2,x,dir1,dir2);
  34.  }
  35. */
  36.  
  37. edge adj_succ(edge e,node v) const 
  38. { return (v==e->s) ? graph::adj_succ(e) : graph::in_succ(e); }
  39.  
  40. edge adj_pred(edge e,node v) const 
  41. { return (v==e->s) ? graph::adj_pred(e) : graph::in_pred(e); }
  42.  
  43. edge cyclic_adj_succ(edge e,node v)  const
  44. { return (v==e->s) ? graph::cyclic_adj_succ(e) : graph::cyclic_in_succ(e); }
  45.  
  46. edge cyclic_adj_pred(edge e,node v) const
  47. { return (v==e->s) ? graph::cyclic_adj_pred(e) : graph::cyclic_in_pred(e); }
  48.  
  49. void print_edge(edge, ostream& = cout) const;
  50.  
  51. ugraph()  { undirected = true; }
  52.  
  53. ugraph(const graph& a)  : graph(a) { undirected = true; }
  54. ugraph(const ugraph& a) : graph(a) { undirected = true; }
  55.  
  56. ~ugraph() { /* ~graph does the job */ }
  57.  
  58. //subgraph constructors
  59. ugraph(ugraph&, const list<node>&, const list<edge>&);
  60. ugraph(ugraph&, const list<edge>&);
  61.  
  62. ugraph& operator=(const ugraph& a) { graph::operator=(a); 
  63.                                      undirected = true;
  64.                                      return *this; }
  65.  
  66. ugraph& operator=(const graph& a)  { graph::operator=(a); 
  67.                                      undirected= true;
  68.                                      return *this;
  69.                                     }
  70.  
  71. };
  72.  
  73.  
  74.  
  75. //------------------------------------------------------------------------------
  76. // UGRAPH: generic ugraphs
  77. //------------------------------------------------------------------------------
  78.  
  79.  
  80. template<class vtype, class etype>
  81.  
  82. class _CLASSTYPE UGRAPH : public ugraph {
  83.  
  84. vtype X;
  85. etype Y;
  86.  
  87. void copy_node_entry(GenPtr& x) const { x=Copy(ACCESS(vtype,x)); }
  88. void copy_edge_entry(GenPtr& x) const { x=Copy(ACCESS(etype,x)); }
  89.  
  90. void clear_node_entry(GenPtr& x) const { Clear(ACCESS(vtype,x)); }
  91. void clear_edge_entry(GenPtr& x) const { Clear(ACCESS(etype,x)); }
  92.  
  93. void write_node_entry(ostream& o, GenPtr& x) const{ Print(ACCESS(vtype,x),o);}
  94. void write_edge_entry(ostream& o, GenPtr& x) const{ Print(ACCESS(etype,x),o);}
  95.  
  96. void read_node_entry(istream& i, GenPtr& x) { Read(X,i); x=Copy(X); }
  97. void read_edge_entry(istream& i, GenPtr& x) { Read(Y,i); x=Copy(Y); }
  98.  
  99. void init_node_entry(GenPtr& x) { Init(X); x = Copy(X); }
  100. void init_edge_entry(GenPtr& x) { Init(Y); x = Copy(Y); }
  101.  
  102. void print_node_entry(ostream& o, GenPtr& x)  const
  103.      { o << "("; Print(ACCESS(vtype,x),o); o << ")"; }
  104. void print_edge_entry(ostream& o, GenPtr& x)  const
  105.      { o << "("; Print(ACCESS(etype,x),o); o << ")"; }
  106.  
  107. char* node_type()  const { return TYPE_NAME(vtype); }
  108. char* edge_type()  const { return TYPE_NAME(etype); }
  109.  
  110. public:
  111.  
  112. int cmp_node_entry(node x, node y) const { return compare(inf(x),inf(y)); }
  113. int cmp_edge_entry(edge x, edge y) const { return compare(inf(x),inf(y)); }
  114.  
  115. vtype  inf(node v)         const { return ACCESS(vtype,ugraph::inf(v)); }
  116. etype  inf(edge e)         const { return ACCESS(etype,ugraph::inf(e)); }
  117. vtype& operator[] (node v)       { return ACCESS(vtype,entry(v)); }
  118. vtype  operator[] (node v) const { return ACCESS(vtype,ugraph::inf(v)); }
  119. etype& operator[] (edge e)       { return ACCESS(etype,entry(e)); }
  120. etype  operator[] (edge e) const { return ACCESS(etype,ugraph::inf(e)); }
  121. void   assign(node v,vtype x)    { graph::assign(v,Convert(x)); }
  122. void   assign(edge e,etype x)    { graph::assign(e,Convert(x)); }
  123.  
  124.  
  125. node new_node(vtype a) { return graph::new_node(Copy(a)); }
  126.  
  127. edge new_edge(node v, node w, etype a) { return ugraph::new_edge(v,w,Copy(a)); }
  128.  
  129. void clear() { clear_all_entries(); ugraph::clear(); }
  130.  
  131. UGRAPH<vtype,etype>& operator=(const UGRAPH<vtype,etype>& a)
  132. { clear_all_entries();
  133.   ugraph::operator=(*(ugraph*)&a);
  134.   copy_all_entries();
  135.   return *this;}
  136.  
  137. UGRAPH<vtype,etype>& operator=(const graph& a)
  138. { clear_all_entries();
  139.   ugraph::operator=(a);
  140.   copy_all_entries();
  141.   return *this;
  142.  }
  143.  
  144. UGRAPH() {}
  145.  
  146. UGRAPH(const UGRAPH<vtype,etype>& a): ugraph(*(ugraph*)&a) 
  147. { copy_all_entries(); }
  148.  
  149. UGRAPH(const graph& a) : ugraph(a)            
  150. { copy_all_entries(); }
  151.  
  152. ~UGRAPH() 
  153. { if (parent==0) clear_all_entries(); }
  154.  
  155. };
  156.  
  157.  
  158.  
  159. extern void complete_ugraph(ugraph&,int);
  160. extern void random_ugraph(ugraph&,int,int);
  161. extern void test_ugraph(ugraph&);
  162.  
  163. #ifndef __ZTC__
  164. inline void complete_graph(ugraph& U,int n)     { complete_ugraph(U,n); }
  165. inline void random_graph(ugraph& U,int n,int m) { random_ugraph(U,n,m); }
  166. inline void test_graph(ugraph& U)               { test_ugraph(U); }
  167. #endif
  168.  
  169.  
  170. #endif
  171.